<?php
/* SVN FILE: $Id: installer_controller.php 250 2010-06-17 10:55:41Z gregory.rome $ */
/**
 * Insttaler Controller.
 *
 * The Installer Controller is in charge of the installation of cBBweb.
 *
 * cBBweb :  digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource    installer_controller.php
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 250 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-17 10:55:41 +0000 (Thu, 17 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Application Controller.
 *
 * @package       cbbweb
 */
uses('model' . DS . 'connection_manager');

class InstallerController extends AppController {
    var $uses = array();

    function beforeFilter() {
        if (file_exists(TMP.'installed.txt')) {
            echo 'Application already installed. Remove app/config/installed.txt to reinstall the application';
            exit();
        }
    }

    function index() {
    }

    function database() {
        $db = ConnectionManager::getDataSource('default');

        if(!$db->isConnected()) {
            echo 'Could not connect to database. Please check the settings in app/config/database.php and try again';
            exit();
        }

        $this->__executeSQLScript($db, CONFIGS.'sql'.DS.'cbbweb.sql');

        $this->redirect('/installer/thanks');
    }

    function thanks() {
        file_put_contents(CONFIGS.'installed.txt', date('Y-m-d, H:i:s'));
    }

    function __executeSQLScript($db, $fileName) {
        $statements = file_get_contents($fileName);
        $statements = explode(';', $statements);

        foreach ($statements as $statement) {
            if (trim($statement) != '') {
                $db->query($statement);
            }
        }
    }
}
?>